﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace SimpleUI02
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        [DllImport("workstation.dll", EntryPoint = "socket_init", CallingConvention = CallingConvention.Cdecl)]
        public static extern int socket_init();
        private List<SensorData> sensorList = new List<SensorData>()
        {
                new SensorData(){SensorValue=23.2, Status="正常"},
                new SensorData(){SensorValue=48, Status="正常"},
                new SensorData(){SensorValue=22, Status="优"},
                new SensorData(){SensorValue=4, Status="二级"},
                new SensorData(){SensorValue=32, Status="安静"},
                new SensorData(){SensorValue=970, Status="正常"},
                new SensorData(){SensorValue=71, Status="正常"},
                new SensorData(){SensorValue=36.7, Status="正常"},
                new SensorData(){SensorValue=7461, Status=null},
        };
        private RecommendationSystem Recommendations = new RecommendationSystem() { Recommendation = "无" };
        private Timer TimeTimer = new Timer();
        public MainWindow()
        {
            InitializeComponent();
            InitializeSensor();
            InitializeBinding();
            TimeTimer.TimerStart(1);
            if (socket_init() != 0) while (true) ;
            DataStart(1);
        }
        public void InitializeSensor()
        {
            for (int i = 0; i < 9; i++)
            {
                sensorList[i].SetTypeNumber(i);
            }
        }
        public void InitializeBinding()
        {
            TemperatureTextBlock.SetBinding(TextBlock.TextProperty, new Binding("SensorValue") { Source = sensorList[0] });
            TemperatureStatusTextBlock.SetBinding(TextBlock.TextProperty, new Binding("Status") { Source = sensorList[0] });
            HumidityTextBlock.SetBinding(TextBlock.TextProperty, new Binding("SensorValue") { Source = sensorList[1] });
            HumidityStatusTextBlock.SetBinding(TextBlock.TextProperty, new Binding("Status") { Source = sensorList[1] });
            PMTextBlock.SetBinding(TextBlock.TextProperty, new Binding("SensorValue") { Source = sensorList[2] });
            PMStatusTextBlock.SetBinding(TextBlock.TextProperty, new Binding("Status") { Source = sensorList[2] });
            UltravioletTextBlock.SetBinding(TextBlock.TextProperty, new Binding("SensorValue") { Source = sensorList[3] });
            UltravioletStatusTextBlock.SetBinding(TextBlock.TextProperty, new Binding("Status") { Source = sensorList[3] });
            NoiseTextBlock.SetBinding(TextBlock.TextProperty, new Binding("SensorValue") { Source = sensorList[4] });
            NoiseStatusTextBlock.SetBinding(TextBlock.TextProperty, new Binding("Status") { Source = sensorList[4] });
            AtmosphericPressureTextBlock.SetBinding(TextBlock.TextProperty, new Binding("SensorValue") { Source = sensorList[5] });
            AtmosphericStatusPressureTextBlock.SetBinding(TextBlock.TextProperty, new Binding("Status") { Source = sensorList[5] });
            HeartRateTextBlock.SetBinding(TextBlock.TextProperty, new Binding("SensorValue") { Source = sensorList[6] });
            HeartRateStatusTextBlock.SetBinding(TextBlock.TextProperty, new Binding("Status") { Source = sensorList[6] });
            BodyTemperatureTextBlock.SetBinding(TextBlock.TextProperty, new Binding("SensorValue") { Source = sensorList[7] });
            BodyTemperatureStatusTextBlock.SetBinding(TextBlock.TextProperty, new Binding("Status") { Source = sensorList[7] });
            StepTextBlock.SetBinding(TextBlock.TextProperty, new Binding("SensorValue") { Source = sensorList[8] });
            RecommendationTextBlock.SetBinding(TextBlock.TextProperty, new Binding("Recommendation") { Source = Recommendations });
            TimerTextBlock.SetBinding(TextBlock.TextProperty, new Binding("TimeNowToString") { Source = TimeTimer });
        }
        public void DataStart(double n)
        {   
            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(n);
            timer.Tick += Data_Tick;
            timer.Start();
        }

        private void Data_Tick(object sender, EventArgs e)
        {
            foreach (SensorData sensorData in sensorList)
            {
                sensorData.GetData();
            }
            Recommendations.GiveRecommendation(sensorList);
        }

        private void TemperatureButton_Click(object sender, RoutedEventArgs e)
        {
            sensorList[0].ResetData();
            Recommendations.GiveRecommendation(sensorList);
        }
        private void HumidityButton_Click(object sender, RoutedEventArgs e)
        {
            sensorList[1].ResetData();
            Recommendations.GiveRecommendation(sensorList);
        }
        private void PMButton_Click(object sender, RoutedEventArgs e)
        {
            sensorList[2].ResetData();
            Recommendations.GiveRecommendation(sensorList);
        }
        private void UltravioletButton_Click(object sender, RoutedEventArgs e)
        {
            sensorList[3].ResetData();
            Recommendations.GiveRecommendation(sensorList);
        }
        private void NoiseButton_Click(object sender, RoutedEventArgs e)
        {
            sensorList[4].ResetData();
            Recommendations.GiveRecommendation(sensorList);
        }
        private void AtmosphericPressureButton_Click(object sender, RoutedEventArgs e)
        {
            sensorList[5].ResetData();
            Recommendations.GiveRecommendation(sensorList);
        }
        private void HeartRateButton_Click(object sender, RoutedEventArgs e)
        {
            sensorList[6].ResetData();
            Recommendations.GiveRecommendation(sensorList);
        }
        private void BodyTemperatureButton_Click(object sender, RoutedEventArgs e)
        {
            sensorList[7].ResetData();
            Recommendations.GiveRecommendation(sensorList);
        }
        private void StepRateButton_Click(object sender, RoutedEventArgs e)
        {
            sensorList[8].ResetData();
        }
    }
}
